/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.odg.dg.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.StringEscapeUtils;
import org.opu.odg.dg.xml.TextMarkupUtil.Lexem;
import org.opu.odg.dg.xml.TextMarkupUtil.LexemType;

/**
 *
 * @author yura
 */
public class XmlTag extends Element{

    private String name;
    private Properties attributes;
    private List<Element> childrens = new ArrayList<Element>();

    public XmlTag(XmlTag parent, String name, Properties attributes) {
        super(parent);
        this.name = name;
        this.attributes = attributes;
    }

    public List<Element> getChildrens() {
        return childrens;
    }

    public Properties getAttributes() {
        return attributes;
    }

    public String getName() {
        return name;
    }

    /**
     * @return the same element that was added
     */
    public <T extends Element>T addChildren(T e) {
        childrens.add(e);
        return e;
    }

    @Override
    public String asAString() {
        StringBuilder sb = getTagTitle();

        if (childrens.isEmpty()) {
            sb.append("/>");
        } else {
            sb.append('>');
            for (Element element : childrens) {
                sb.append(element.asAString());
            }
            sb.append("</").append(name).append('>');
        }

        return sb.toString();
    }

    @Override
    public List<Lexem> toLexems() {
        List<Lexem> list = new ArrayList<Lexem>();
        StringBuilder sb = getTagTitle();

        if (childrens.isEmpty()) {
            sb.append("/>");
            list.add(new Lexem(sb.toString(), LexemType.TEXT));
        } else {
            sb.append(">");
            list.add(new Lexem(sb.toString(), LexemType.TEXT));
            for (Element element : childrens) {
                list.addAll(element.toLexems());
            }
            list.add(new Lexem("</" + name + '>', LexemType.TEXT));
        }

        return list;
    }

    @Override
    public String toString() {
        return String.valueOf(getName());
    }

    private StringBuilder getTagTitle() {
        StringBuilder sb = new StringBuilder();
        sb.append('<').append(name);
        for (String string : attributes.stringPropertyNames()) {
            sb.append(' ').append(string).append("=\"").append(StringEscapeUtils
                    .escapeXml(attributes.getProperty(string))).append('"');
        }
        return sb;
    }
}
